package dao;

import util.JdbcUtils;
import util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 院系专业表的dao
 *
 * @author 邓晴予
 */
public class DeptMajorDao {
    /**
     * 通过专业名，找到其所属的院系名
     * 说明：可能不同的院系有相同名字的专业
     *
     * @param major 专业名
     * @return 专业所属院系名 dept by major
     * @author 邓晴予
     */
    public List<Map<String, String>> getDeptByMajor(String major) {
        String sql = "select dept.name from dept_major major, dept_major dept where major.parentId=dept.id and major.name=?";

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(major)) {
            para.add(major);
        }
        return JdbcUtils.queryForMap(sql, para);
    }

    /**
     * 通过专业id，找到其所属的院系名
     *
     * @param majorId 专业id
     * @return 专业所属院系名 dept by major id
     * @author 邓晴予
     */
    public String getDeptByMajorId(String majorId) {
        String sql = "select dept.name from dept_major major, dept_major dept where major.parentId=dept.id and major.id=?";

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(majorId)) {
            para.add(majorId);
        }
        List<Map<String, String>> list = JdbcUtils.queryForMap(sql, para);
        return list.isEmpty() ? "" : list.get(0).get("name");
    }

    /**
     * 通过majorid获得major名
     *
     * @param majorId the major id
     * @return major by id
     */
    public String getMajorById(String majorId) {
        String sql = "select name from dept_major where id=?";

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(majorId)) {
            para.add(majorId);
        }
        List<Map<String, String>> list = JdbcUtils.queryForMap(sql, para);
        return list.isEmpty() ? "" : list.get(0).get("name");
    }

    /**
     * 判断major是否属于dept
     *
     * @param major the major
     * @param dept  the dept
     * @return boolean
     * @author 邓晴予
     */
    public boolean isMajorBelongDept(String major, String dept) {
        String sql = "select count(*) as total from dept_major dm where dm.id= ? and dm.parentId = ?";

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(major)) {
            para.add(major);
        }
        if (StringUtil.isNotEmpty(dept)) {
            para.add(dept);
        }
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);

        return !result.isEmpty() && Integer.parseInt(result.get(0).get("total")) == 1;
    }

    /**
     * 通过专业名、院系名找到专业的id
     *
     * @param major the major
     * @param dept  the dept
     * @return major id
     * @author 邓晴予
     */
    public int getMajorId(String major, String dept) {
        String sql = "select major.id from dept_major major, dept_major dept where major.parentId=dept.id and major.name=? and dept.name=?";

        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(major)) {
            para.add(major);
        }
        if (StringUtil.isNotEmpty(dept)) {
            para.add(dept);
        }
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);
        return result.isEmpty() ? 0 : Integer.parseInt(result.get(0).get("id"));
    }

    /**
     * Gets departments.
     *
     * @return the departments
     */
    public List<Map<String, String>> getDepartments() {
        String sql = "SELECT id, `name` FROM dept_major WHERE parentId IS NULL ";
        return JdbcUtils.queryForMap(sql, null);
    }

    /**
     * Gets majors.
     *
     * @param deptId the dept id
     * @return the majors
     */
    public List<Map<String, String>> getMajors(String deptId) {
        String sql = "SELECT id, `name` FROM dept_major WHERE parentId = ?";
        ArrayList<Object> para = new ArrayList<>();
        para.add(deptId);
        return JdbcUtils.queryForMap(sql, para);
    }
}

